<?php
error_reporting(E_ALL);
ini_set('display_errors', '1'); 
header("Content-Type:text/html;charset=UTF-8");
//发送短信
function  sms_send($mobile,$content){
      //要post的数据
      $content=  iconv("utf-8", "gbk", $content);
      $flag = 0;
      $argv = array(
           'sn'=>'SDK-CKS-010-00322', ////替换成您自己的序列号
           'pwd'=>strtoupper(md5('SDK-CKS-010-00322'.'b_45e-d2')), //此处密码需要加密 加密方式为 md5(sn+password) 32位大写
           'mobile'=>$mobile,//手机号 多个用英文的逗号隔开 post理论没有长度限制.推荐群发一次小于等于10000个手机号
           'content'=>$content,
           'ext'=>'',
           'stime'=>'',//定时时间 格式为2011-6-29 11:09:21
           'rrid'=>''
      );

      //构造要post的字符串,构造$argv,通过socket连接的方式发送
      $params = ''; // add by qplanr
      foreach ($argv as $key=>$value) {
        if ($flag!=0) {
          $params .= "&";
          $flag = 1;
        }
        $params.= $key."="; $params.= urlencode($value);
        $flag = 1;
      }

      //var_dump($yzm, $params);exit;
      $length = strlen($params);

      //创建socket连接
      $fp = fsockopen("sdk2.entinfo.cn",8060,$errno,$errstr,10) or exit($errstr."--->".$errno);

      //构造post请求的头
      $header = "POST /webservice.asmx/mt HTTP/1.1\r\n";
      $header .= "Host:sdk2.entinfo.cn\r\n";
      $header .= "Content-Type: application/x-www-form-urlencoded\r\n";
      $header .= "Content-Length: ".$length."\r\n";
      $header .= "Connection: Close\r\n\r\n";
      //添加post的字符串
      $header .= $params."\r\n";

      //发送post的数据
      fputs($fp,$header);
      $inheader = 1;
      while (!feof($fp)) {
        $line = fgets($fp,1024); //去除请求包的头只显示页面的返回数据
        if ($inheader && ($line == "\n" || $line == "\r\n")) {
           $inheader = 0;
        }
        if ($inheader == 0) {
          // echo $line;
        }
      }
      //<string xmlns="http://tempuri.org/">-5</string>
      $line=str_replace("<string xmlns=\"http://tempuri.org/\">","",$line);
      $line=str_replace("</string>","",$line);
      $result=explode("-",$line);

      if(count($result)>1){
                    $arr['status'] = 0;
                    $arr['info'] = "动态密码发送失败";
        //echo '发送失败返回值为:'.$line.'。请查看webservice返回值对照表';
      }else{

                $arr['status'] = 1;
                $arr['info'] = "动态密码发送成功";
        
      }
      return $arr;
} 
function sendmessage($mobile,$msg) {

  if (empty($msg)) {
    return '短信内容为空？写点吧～';
  }
  
  $api_send_url = 'http://222.73.117.158/msg/HttpBatchSendSM';
 
  //创蓝接口参数
  $postArr = array (
            'account' =>'louyouhui',
            'pswd' =>'Tch123456',
            'msg' =>$msg,
            'mobile' =>$mobile,
            'needstatus' =>'true',
            'product' =>'',
            'extno' => '',
         );
  
  $result = curlPost($api_send_url,$postArr);
    $result = execResult($result);
    if($result[1]==0){
    return true;
  }else{
    return false;
  }
}
function getClientIP(){  
        global $ip;  
        if (getenv("HTTP_CLIENT_IP"))  
            $ip = getenv("HTTP_CLIENT_IP");  
        else if(getenv("HTTP_X_FORWARDED_FOR"))  
            $ip = getenv("HTTP_X_FORWARDED_FOR");  
        else if(getenv("REMOTE_ADDR"))  
            $ip = getenv("REMOTE_ADDR");  
        else $ip = "Unknow";  
        return $ip;  
} 
 
/**
 * 处理返回值
 * 
 */
function execResult($result){
  $result=preg_split("/[,\r\n]/",$result);
  return $result;
}

/**
 * 通过CURL发送HTTP请求
 * @param string $url  //请求URL
 * @param array $postFields //请求参数 
 * @return mixed
 */
function curlPost($url,$postFields){
  $postFields = http_build_query($postFields);
  $ch = curl_init ();
  curl_setopt ( $ch, CURLOPT_POST, 1 );
  curl_setopt ( $ch, CURLOPT_HEADER, 0 );
  curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, 1 );
  curl_setopt ( $ch, CURLOPT_URL, $url );
  curl_setopt ( $ch, CURLOPT_POSTFIELDS, $postFields );
  $result = curl_exec ( $ch );
  curl_close ( $ch );
  return $result;
}
/**
 * 字符串转换为数组，主要用于把分隔符调整到第二个参数
 * @param  string $str  要分割的字符串
 * @param  string $glue 分割符
 * @return array
 * @author 麦当苗儿 <zuojiazi@vip.qq.com>
 */
function str2arr($str, $glue = ','){
    return explode($glue, $str);
}

/**
 * 数组转换为字符串，主要用于把分隔符调整到第二个参数
 * @param  array  $arr  要连接的数组
 * @param  string $glue 分割符
 * @return string
 * @author 麦当苗儿 <zuojiazi@vip.qq.com>
 */
function arr2str($arr, $glue = ','){
    return implode($glue, $arr);
}

/**
 * 字符串截取，支持中文和其他编码
 * @static
 * @access public
 * @param string $str 需要转换的字符串
 * @param string $start 开始位置
 * @param string $length 截取长度
 * @param string $charset 编码格式
 * @param string $suffix 截断显示字符
 * @return string<td>{$v.description|msubstr=###,0,50}
 */
function msubstr($str, $start=0, $length) {
  $charset="utf-8";
  $suffix=true;
    if(function_exists("mb_substr"))
        $slice = mb_substr($str, $start, $length, $charset);
    elseif(function_exists('iconv_substr')) {
        $slice = iconv_substr($str,$start,$length,$charset);
        if(false === $slice) {
            $slice = '';
        }
    }else{
        $re['utf-8']   = "/[\x01-\x7f]|[\xc2-\xdf][\x80-\xbf]|[\xe0-\xef][\x80-\xbf]{2}|[\xf0-\xff][\x80-\xbf]{3}/";
        $re['gb2312'] = "/[\x01-\x7f]|[\xb0-\xf7][\xa0-\xfe]/";
        $re['gbk']    = "/[\x01-\x7f]|[\x81-\xfe][\x40-\xfe]/";
        $re['big5']   = "/[\x01-\x7f]|[\x81-\xfe]([\x40-\x7e]|\xa1-\xfe])/";
        preg_match_all($re[$charset], $str, $match);
        $slice = join("",array_slice($match[0], $start, $length));
    }
    return $suffix ? $slice.'' : $slice;
}

     function rmdirs($dir){

         $dir_arr = scandir($dir);
         foreach($dir_arr as $key=>$val){
             if($val == '.' || $val == '..'){}
             else {
                 if(is_dir($dir.'/'.$val))    
                 {                            
                     if(@rmdir($dir.'/'.$val) == 'true'){}                   
                     else
                     rmdirs($dir.'/'.$val);                    
                 }
                 else                
                 unlink($dir.'/'.$val);
             }
         }
     }
function gettime(){
  return date('Y-m-d H:i:s',time());
}


function encrypt($data) {
        //return md5(C('AUTH_MASK') . md5($data));
    return md5(md5($data));
}

//html代码输出
function html_out($str){
    if(function_exists('htmlspecialchars_decode')){
        $str=htmlspecialchars_decode($str);
    }else{
        $str=html_entity_decode($str);
    }
    $str = stripslashes($str);
    return $str;
}



/**
 * 取得文件扩展
 * @param type $filename 文件名
 * @return type 后缀
 */
function fileext($filename) {
    $pathinfo = pathinfo($filename);
    return $pathinfo['extension'];
}

/**
 * 根据文件扩展名来判断是否为图片类型
 * @param type $file 文件名
 * @return type 是图片类型返回 true，否则返回 false
 */
function isImage($file) {
    $ext_arr = array('jpg', 'gif', 'png', 'bmp', 'jpeg', 'tiff');
    //取得扩展名
    $ext = fileext($file);
    return in_array($ext, $ext_arr) ? true : false;
}

function escape($str) {
          preg_match_all("/[\x80-\xff].|[\x01-\x7f]+/",$str,$r);
    $ar = $r[0];
          foreach($ar as $k=>$v) {
          if(ord($v[0]) < 128)
              $ar[$k] = rawurlencode($v);
          else
              $ar[$k] = "%u".bin2hex(iconv("GB2312","UCS-2",$v));
          }
          return join("",$ar);
}
function unescape($str)
      {
          $ret = '';
          $len = strlen($str);
          for ($i = 0; $i < $len; $i++){
              if ($str[$i] == '%' && $str[$i+1] == 'u'){
                  $val = hexdec(substr($str, $i+2, 4));
                  if ($val < 0x7f) $ret .= chr($val);
                  else if($val < 0x800) $ret .= chr(0xc0|($val>>6)).chr(0x80|($val&0x3f));
                  else $ret .= chr(0xe0|($val>>12)).chr(0x80|(($val>>6)&0x3f)).chr(0x80|($val&0x3f));
                  $i += 5;
              }else if ($str[$i] == '%'){
                  $ret .= urldecode(substr($str, $i, 3));
                  $i += 2;
              }else $ret .= $str[$i];
          }
          return $ret;
}
function getDistance($lat1, $lon1, $lat2, $lon2){
    //将角度转为狐度
    $radLat1=deg2rad($lat1);//deg2rad()函数将角度转换为弧度
    $radLat2=deg2rad($lat2);
    $radLng1=deg2rad($lon1);
    $radLng2=deg2rad($lon2);
    $a=$radLat1-$radLat2;
    $b=$radLng1-$radLng2;
    $s=2*asin(sqrt(pow(sin($a/2),2)+cos($radLat1)*cos($radLat2)*pow(sin($b/2),2)))*6378.137;
    return $s;
}
function booking_price_new($cid,$time,$bid=0){
  $db=new db;
  $data=array();
  $week=date('w',$time);
  $where['cid']=$cid;
  $where['status']=1;
  $where['membership']=$bid;
  $booking_price=$db->table('booking_price')->where($where)->order('type asc,edittime asc')->select();

$booking_force_time=$db->table('booking_force_time')->select();
$time_arr=array();
if(!empty($booking_force_time)) {
  foreach ($booking_force_time as $ko => $vo) {
      $time_arr[]=$vo['time'];
  }
}
      $time1=date("Y-m-d",$time);
      $time=strtotime($time1);
    if (!empty($booking_price)) { 
             if(in_array($time,$time_arr)) {
                      $whe['time']=$time;
                      $force=$db->table('booking_force_time')->where($whe)->find();
                      if (!empty($force)) {
                        $type=$force['force_type'];
                         if($type==0) {
                                  $whe1['type']=0;
                                  $whe1['cid']=$cid;
                                  $whe1['membership']=$bid;
                                  $vc=$db->table('booking_price')->where($whe1)->order('edittime desc')->select();
                                      $data=$vc;     
                        }elseif($type==1) {
                                 $whe1['type']=1;
                                 $whe1['cid']=$cid;
                                 $whe1['membership']=$bid;
                                  $vc=$db->table('booking_price')->where($whe1)->order('edittime desc')->select();
                                      $data=$vc;       
                        }
                      } 
                  }else{
                      foreach($booking_price as $k=>$v) {
                          $type=$v['type'];
                          $validity_star_time=$v['validity_star_time'];
                          $validity_end_time=$v['validity_end_time'];
                          $price=$v['price'];
                          $price_include=$v['price_include'];
                          $booking_id=$v['booking_id'];
                          $week_array=explode(",",$v['week']);
                          
                                if($type==0) {
                                   if ($time>=$validity_star_time && $validity_end_time>=$time) {
                                       if (in_array($week,$week_array)) {
                                          $data[$k]=$v;
                                       }
                                    }
                                }elseif($type==1) {
                                   if ($time>=$validity_star_time && $validity_end_time>=$time) {
                                       if (in_array($week,$week_array)) {
                                          $data[$k]=$v;
                                       }
                                    }
                                }elseif($type==2) {
                                   if ($time>=$validity_star_time && $validity_end_time>=$time) {
                                       if (in_array($week,$week_array)) {
                                          $data[$k]=$v;
                                       }
                                    }
                                }elseif($type==3){
                                    if ($time>=$validity_star_time && $validity_end_time>=$time) {
                                       if (in_array($week,$week_array)) {
                                          $data[$k]=$v;
                                       }
                                    }
                                   
                                }
                      }  
            }
    }
    return $data;
}
function booking_price($cid,$time){
  $db=new db;
  $data=array();
  $week=date('w',$time);
  $where['cid']=$cid;
  $where['status']=1;
  //$where['limit_people']=0;
  //$where['pay_mode']=1;
  $where['membership']=0;
  $booking_price=$db->table('booking_price')->where($where)->order('type asc,price desc')->select();

$booking_force_time=$db->table('booking_force_time')->select();
$time_arr=array();
if(!empty($booking_force_time)) {
  foreach ($booking_force_time as $ko => $vo) {
      $time_arr[]=$vo['time'];
  }
}
      $time1=date("Y-m-d",$time);
      $time=strtotime($time1);
    if (!empty($booking_price)) { 
            foreach($booking_price as $k=>$v) {
                $type=$v['type'];
                $validity_star_time=$v['validity_star_time'];
                $validity_end_time=$v['validity_end_time'];
                $price=$v['price'];
                $price_include=$v['price_include'];
                $booking_id=$v['booking_id'];
                $week_array=explode(",",$v['week']);
                 if(in_array($time,$time_arr)) {
                      $whe['time']=$time;
                      $force=$db->table('booking_force_time')->where($whe)->find();
                      if (!empty($force)) {
                        $type=$force['force_type'];
                         if($type==0) {
                                  $whe1['type']=0;
                                  $whe1['cid']=$cid;
                                  $whe1['membership']=0;
                                  $vc=$db->table('booking_price')->field('id,cid,booking_id,type,validity_star_time,validity_end_time,week,price,price_include')->where($whe1)->order('price asc')->find();
                                      $data=$vc;     
                        }elseif($type==1) {
                                 $whe1['type']=1;
                                 $whe1['cid']=$cid;
                                 $whe1['membership']=0;
                                  $vc=$db->table('booking_price')->field('id,cid,booking_id,type,validity_star_time,validity_end_time,week,price,price_include')->where($whe1)->order('price asc')->find();
                                      $data=$vc;       
                        }
                      } 
                  }else{
                      if($type==0) {
                         if ($time>=$validity_star_time && $validity_end_time>=$time) {
                             if (in_array($week,$week_array)) {
                                $data=$v;
                             }
                          }
                      }elseif($type==1) {
                         if ($time>=$validity_star_time && $validity_end_time>=$time) {
                             if (in_array($week,$week_array)) {
                                $data=$v;
                             }
                          }
                      }elseif($type==2) {
                         if ($time>=$validity_star_time && $validity_end_time>=$time) {
                             if (in_array($week,$week_array)) {
                                $data=$v;
                             }
                          }
                      }elseif($type==3){
                          if ($time>=$validity_star_time && $validity_end_time>=$time) {
                             if (in_array($week,$week_array)) {
                                $data=$v;
                             }
                          }
                         
                      }
                  }  
            }
    }
    return $data;
}
function booking_price_vip($cid,$time,$bid){
  $db=new db;
  $data=array();
  $week=date('w',$time);
  $where['cid']=$cid;
  $where['status']=1;
  $where['membership']=$bid;
  $booking_price=$db->table('booking_price')->where($where)->order('type asc,price desc')->select();

$booking_force_time=$db->table('booking_force_time')->select();
$time_arr=array();
if(!empty($booking_force_time)) {
  foreach ($booking_force_time as $ko => $vo) {
      $time_arr[]=$vo['time'];
  }
}
      $time1=date("Y-m-d",$time);
      $time=strtotime($time1);
    if (!empty($booking_price)) { 
            foreach($booking_price as $k=>$v) {
                $type=$v['type'];
                $validity_star_time=$v['validity_star_time'];
                $validity_end_time=$v['validity_end_time'];
                $price=$v['price'];
                $price_include=$v['price_include'];
                $booking_id=$v['booking_id'];
                $week_array=explode(",",$v['week']);
                 if(in_array($time,$time_arr)) {
                      $whe['time']=$time;
                      $force=$db->table('booking_force_time')->where($whe)->find();
                      if (!empty($force)) {
                        $type=$force['force_type'];
                         if($type==0) {
                                  $whe1['type']=0;
                                  $whe1['cid']=$cid;
                                  $whe1['membership']=$bid;
                                  $vc=$db->table('booking_price')->where($whe1)->order('price asc')->find();
                                      $data=$vc;     
                        }elseif($type==1) {
                                 $whe1['type']=1;
                                 $whe1['cid']=$cid;
                                 $whe1['membership']=$bid;
                                  $vc=$db->table('booking_price')->where($whe1)->order('price asc')->find();
                                      $data=$vc;       
                        }
                      } 
                  }else{
                      if($type==0) {
                         if ($time>=$validity_star_time && $validity_end_time>=$time) {
                             if (in_array($week,$week_array)) {
                                $data=$v;
                             }
                          }
                      }elseif($type==1) {
                         if ($time>=$validity_star_time && $validity_end_time>=$time) {
                             if (in_array($week,$week_array)) {
                                $data=$v;
                             }
                          }
                      }elseif($type==2) {
                         if ($time>=$validity_star_time && $validity_end_time>=$time) {
                             if (in_array($week,$week_array)) {
                                $data=$v;
                             }
                          }
                      }elseif($type==3){
                          if ($time>=$validity_star_time && $validity_end_time>=$time) {
                             if (in_array($week,$week_array)) {
                                $data=$v;
                             }
                          }
                         
                      }
                  }  
            }
    }
    return $data;
}
function booking_price_vip_new($cid,$time){
  $db=new db;
  $data=array();
  $week=date('w',$time);
  $where['cid']=$cid;
  $where['status']=1;
  $where['membership']=array("gt",0);
  $booking_price=$db->table('booking_price')->where($where)->order('type asc,price desc')->select();

$booking_force_time=$db->table('booking_force_time')->select();
$time_arr=array();
if(!empty($booking_force_time)) {
  foreach ($booking_force_time as $ko => $vo) {
      $time_arr[]=$vo['time'];
  }
}
      $time1=date("Y-m-d",$time);
      $time=strtotime($time1);
    if (!empty($booking_price)) { 
            foreach($booking_price as $k=>$v) {
                $type=$v['type'];
                $validity_star_time=$v['validity_star_time'];
                $validity_end_time=$v['validity_end_time'];
                $price=$v['price'];
                $price_include=$v['price_include'];
                $booking_id=$v['booking_id'];
                $week_array=explode(",",$v['week']);
                 if(in_array($time,$time_arr)) {
                      $whe['time']=$time;
                      $force=$db->table('booking_force_time')->where($whe)->find();
                      if (!empty($force)) {
                        $type=$force['force_type'];
                         if($type==0) {
                                  $whe1['type']=0;
                                  $whe1['cid']=$cid;
                                  $vc=$db->table('booking_price')->where($whe1)->order('price asc')->find();
                                      $data=$vc;     
                        }elseif($type==1) {
                                 $whe1['type']=1;
                                 $whe1['cid']=$cid;
                                  $vc=$db->table('booking_price')->where($whe1)->order('price asc')->find();
                                      $data=$vc;       
                        }
                      } 
                  }else{
                      if($type==0) {
                         if ($time>=$validity_star_time && $validity_end_time>=$time) {
                             if (in_array($week,$week_array)) {
                                $data=$v;
                             }
                          }
                      }elseif($type==1) {
                         if ($time>=$validity_star_time && $validity_end_time>=$time) {
                             if (in_array($week,$week_array)) {
                                $data=$v;
                             }
                          }
                      }elseif($type==2) {
                         if ($time>=$validity_star_time && $validity_end_time>=$time) {
                             if (in_array($week,$week_array)) {
                                $data=$v;
                             }
                          }
                      }elseif($type==3){
                          if ($time>=$validity_star_time && $validity_end_time>=$time) {
                             if (in_array($week,$week_array)) {
                                $data=$v;
                             }
                          }
                         
                      }
                  }  
            }
    }
    return $data;
}

function chenck_booking($cid,$uid,$bid,$price_id,$time,$buckle_number,$people_number){
        $db=new db;
        if(!$cid || !$uid || !$bid || !$price_id ||!$time ||!$buckle_number ||!$people_number) {

                return;die;
        }
        $status=0;
        $wher22['id']=$bid;
        $bene=$db->table('benefit')->where($wher22)->find();

        $wher['id']=$bene['btid'];
        $benefit=$db->table('benefit_type')->where($wher)->find();

        $benefit_where['uid']=$uid;
        $benefit_where['status']=1;
        $benefit_where['bid']=$bid;
        $benefit_where['end_time']=array('gt',time());
        $info=$db->table('benefit_order')->field('id,bid,cid,cids')->where($benefit_where)->find();
        //var_dump($_POST);
        if($benefit['if_deduction']){//是否扣次
             $cidstr='';
            if($benefit['if_custom']) {//是否自定义球场
                //查询订单里的cids
                  $cids=explode(',',$info['cids']);

            }else{
                //查询权益卡行权球场
                    $whe['bid']=$bid;
                    $list=$db->table('benefit_court')->where($whe)->select();
                    if(!empty($list)) {
                        foreach ($list as $k=>$v) {
                            $cids[$k]=$v['cid'];
                        }
                        
                         
                    }
            
            }
            if(in_array($cid,$cids)){
                $wher4['uid']=$uid;
                $wher4['bid']=$bid;
                $wher4['cid']=$cid;
                $wher4['cycle_star_time']=array('elt',$time);
                $wher4['cycle_end_time']=array('egt',$time);
                $benefit_info=$db->table('benefit_member')->where($wher4)->find();
         
                if(empty($benefit_info)) {
                   $status=0;
                }else{
                      $ww['id']=$price_id;
                      $booking_price=$db->table('booking_price')->where($ww)->find();

                       
                      
                          if(empty($booking_price)) {
                            $status=0;
                          }else{
                            $d=date("Y-m-d",$time);
                            $t1=strtotime($d);
                            //$t2=strtotime('+1 day',$t1);
                            $t2=$t1+86400;
                            //当日预定总数
                            $wher5['uid']=$uid;
                            $wher5['bid']=$bid;
                            $wher5['status']=array('not in','3,4');
                            $wher5['cid']=$cid;
                            $wher5['playing_time']=array('lt',$t2);
                            $wher5['playing_time']=array('gt',$t1);
                            //$count=$db->table('booking_order')->where($wher5)->count();
                            //$count=$db->table('booking_order')->field('su$db->table(people_number) as sum_people_number')->where($wher5)->find();
                            $count=$db->query("SELECT SUM( people_number ) AS sum_people_number FROM zl_booking_order WHERE uid =$uid AND bid =$bid AND cid =$cid AND status in('0,1,2,5') AND playing_time between $t1 and $t2 LIMIT 1 ");

                            if($count[0]['sum_people_number']) {
                              $sum_people_number=$count[0]['sum_people_number'];
                            }else{
                              $sum_people_number=0;
                            }
                            /*if($count['sum_people_number']>0) {
                              $sum_people_number=$count['sum_people_number'];
                            }else{
                              $sum_people_number=0;
                            }*/

                            $number_count=$benefit_info['day_max_number']-$people_number-$sum_people_number;

                          //return $sum_people_number;die;
                             if($number_count>=0) {
                                  $surplus_holiday_number=$benefit_info['surplus_holiday_number'];
                                  $surplus_ordinary_number=$benefit_info['surplus_ordinary_number'];
                                  $surplus_sum_number=$benefit_info['surplus_sum_number'];
                                 //return $buckle_number;
                                  if($surplus_sum_number>=$buckle_number) {
                                      if($benefit_info['if_distinguish']){
                                          if($booking_price['type']==1) {
                                            
                                            //假日
                                            if ($surplus_holiday_number>=$buckle_number) {
                           
                                              $status=1;
                                            }else{
                                              $status=0;
                                     
                                            }
                                          }elseif($booking_price['type']==0){
                                            //平日
                                           
                                            if($surplus_ordinary_number>=$buckle_number) {
                                              
                                              $status=1;
                                            }else{
                                              $status=0;

                                            }
                                          }
                                      }else{
                                          $status=1;
                                      }
                                  }
                                }
                          }
                        
                }   
            }
        }else{
                  //查询权益卡行权球场
                    $whe['bid']=$bid;
                    $list=$db->table('benefit_court')->where($whe)->select();
                    if(!empty($list)) {
                        foreach ($list as $k=>$v) {
                            $cids[$k]=$v['cid'];
                        }
                        if(in_array($cid,$cids)){
                            $status=1;
                        }
                        
                    }

        }

return $status;


}

function invite_commission($oid){
 $db=new db;
   
    $where64['id']=$oid;
    $order_in=$db->table('benefit_order')->where($where64)->find();
    
    $sum_price=$order_in['sum_price'];
    $uid=intval($order_in['uid']);
    $bid=$order_in['bid'];
    $sum_price=$order_in['sum_price'];
    $invite_uid=intval($order_in['invite_uid']);
     $where63['id']=$bid;
    $in=$db->table('benefit')->where($where63)->find();

    if($in['if_commission']){
            $commission_price=$in['commission_price'];
            $commission=$in['commission'];
            $commission2=$in['commission2'];
            $commission_price1=$commission_price*$commission*0.01;
            $commission_price2=$commission_price*$commission2*0.01;
            if(!$invite_uid  || $invite_uid==$uid){
                $whe22['invite_uid']=$uid;
                $invite=$db->table('invite_register')->where($whe22)->find();
                $invite_uid=intval($invite['uid']);
            }


           
            if($invite_uid>0) {
                  $wher55['uid']=$invite_uid;
                  $account=$db->table('commission_account')->where($wher55)->find();
                  
                  if (!empty($account)){
                    $account_data['uid']=$invite_uid;
                    $account_data['sum_money']=$account['sum_money']+$commission_price1;//总金额
                    $account_data['await_confirm_money']=$account['await_confirm_money']+$commission_price1;//待确认金额
                    $where71['id']=$account['id'];
                    $s=$db->table('commission_account')->data($account_data)->where($where71)->update();
                  }else{
                    $account_data['uid']=$invite_uid;
                    $account_data['sum_money']=$commission_price1;//总金额
                    $account_data['confirm_money']=0;//已确认金额
                    $account_data['await_confirm_money']=$commission_price1;//待确认金额
                    $account_data['apply_money']=0;//申请金额
                    $account_data['pick_money']=0;//提取金额
                    $account_data['await_money']=0;//待提取金额
                    $s=$db->table('commission_account')->data($account_data)->insert();
                  }
                  if($s){
                    $order_data['oid']=$oid;
                    $order_data['uid']=$uid;
                    $order_data['order_money']=$sum_price;
                    $order_data['commission_money']=$commission_price1;
                    $order_data['addtime']=time();
                    $order_data['type']=1;
                    $order_data['level']=1;
                    $order_data['invite_uid']=$invite_uid;
                    $db->table('commission_order')->data($order_data)->insert();

                     //增加变动明细
                      $member_coin['oid']=$oid;
                      $member_coin['uid']=$invite_uid;;
                      $member_coin['title']='推荐奖励';
                      $member_coin['price']=$commission_price1;
                      //$member_coin['happy_coin']=$account['confirm_money'];
                      $member_coin['type']=0;
                      $member_coin['addtime']=time();
                      $member_coin['balance']=1;
                      $member_coin['status']=1;
                      $db->table('member_coin_log')->data($member_coin)->insert();
                  }

                  $whe33['invite_uid']=$invite_uid;
                  $invite22=$db->table('invite_register')->where($whe33)->find();
                  if(!empty($invite22)){
                        $wher55['uid']=$invite22['uid'];
                        $account2=$db->table('commission_account')->where($wher55)->find();
                        if (!empty($account2)){
                          $account_data2['uid']=$invite22['uid'];
                          $account_data2['sum_money']=$account2['sum_money']+$commission_price2;//总金额
                          $account_data2['await_confirm_money']=$account2['await_confirm_money']+$commission_price2;//待确认金额
                          $where72['id']=$account2['id'];
                          $s1=$db->table('commission_account')->data($account_data2)->where($where72)->update();
                        }else{
                          $account_data2['uid']=$invite22['uid'];
                          $account_data2['sum_money']=$commission_price2;//总金额
                          $account_data2['confirm_money']=0;//已确认金额
                          $account_data2['await_confirm_money']=$commission_price2;//待确认金额
                          $account_data2['apply_money']=0;//申请金额
                          $account_data2['pick_money']=0;//提取金额
                          $account_data2['await_money']=0;//待提取金额
                          $s1=$db->table('commission_account')->data($account_data2)->insert();
                        }
                        if($s1) {
                          $order_data2['oid']=$oid;
                          $order_data2['uid']=$uid;
                          $order_data2['order_money']=$sum_price;
                          $order_data2['commission_money']=$commission_price2;
                          $order_data2['addtime']=time();
                          $order_data2['type']=1;
                          $order_data2['level']=2;
                          $order_data2['invite_uid']=$invite22['uid'];
                          $db->table('commission_order')->data($order_data2)->insert();
                          //增加变动明细
                          $member_coin2['oid']=$oid;
                          $member_coin2['uid']=$account2['uid'];
                          $member_coin2['title']='推荐奖励';
                          $member_coin2['price']=$commission_price2;
                          //$member_coin2['happy_coin']=$account2['confirm_money'];
                          $member_coin2['type']=0;
                          $member_coin2['addtime']=time();
                          $member_coin2['balance']=1;
                          $member_coin2['status']=1;
                          $db->table('member_coin_log')->data($member_coin2)->insert();
                        }
                    
                  }
            }

    }



  return $s;

}
function wechatmsg($order_num,$type){
        $curl = curl_init();  
        //设置抓取的url  
        //curl_setopt($curl, CURLOPT_URL, 'http://192.168.0.225:8088/kldgolf/matchScore/updateMatchRanking'); 
        curl_setopt($curl, CURLOPT_URL, 'http://www.hapgolf.com/hapgolf/index.php/wechat/msg/api'); 
        //设置头文件的信息作为数据流输出  
        //curl_setopt($curl, CURLOPT_HEADER, 1);
        //设置获取的信息以文件流的形式返回，而不是直接输出。  
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);  
        //设置post方式提交  
        curl_setopt($curl, CURLOPT_POST, 1);
        //设置post数据  
        $post_data="type=$type&order_num=$order_num";
        //post提交的数据
        curl_setopt($curl, CURLOPT_POSTFIELDS, $post_data);
        //执行命令  
        $data = curl_exec($curl);
        //关闭URL请求  
        curl_close($curl);
        //显示获得的数据  
         //echo $data;die;
        $list = json_decode($data);
        return $list;


}
function activation($id){
        $db=new db;
        if($id){
            $whe123['id']=$id;
            $data=$db->table('benefit_order')->where($whe123)->find();
            
            $dd=date("Y-m-d");
            $time=strtotime($dd);
            $bid=$data['bid'];
            $years=intval($data['years']);
            $data['activate_time']=$time;
            $data['end_time']=strtotime("+$years year",$time);
            $data['status']=1;
            
            //生成权益账户
            $where1['id']=$bid;
            $benefit_info=$db->table('benefit')->where($where1)->find();
            $wher21['id']=$benefit_info['btid'];
            $benefit_type=$db->table('benefit_type')->where($wher21)->find();
            
            if($benefit_type['if_deduction']) {
              //扣次
              $benefit_config=$db->table('benefit_config');
              if($benefit_info['package_id']) {
              //是否配置权益包
                $wh1['package_id']=$benefit_info['package_id'];
                $benefit_list=$db->table('benefit_config')->where($wh1)->select();
               
                if (!empty($benefit_list)) {
                  
                  foreach ($benefit_list as $k => $v) {
                    $if_distinguish=intval($v['if_distinguish']);
                    $if_practice=intval($v['if_practice']);
                    if ($if_distinguish) {
                        $holiday_number=$v['holiday_number'];
                        $ordinary_number=$v['ordinary_number'];
                    }

                    //$cid=$v['cid'];
                    $cids=$v['cid'];
                    $array=explode(',',$cids); 
                    foreach ($array as $key => $value) {
                        $cid=$value;
                       ##################
                         if($v['years']!=$benefit_info['years']){
                                   if($if_practice==1) {
                                        #####
                                       $years1=intval($v['years']);
                                          $bdata['cycle_years']=$years1;
                                          $bdata['truename']=$data['truename'];
                                          $bdata['cycle_star_time']=$time;
                                          $bdata['cycle_end_time']=strtotime("+$years1 year",$time);
                                          $bdata['bid']=$bid;
                                          $bdata['boid']=$id;
                                          $bdata['uid']=$data['uid'];
                                          $bdata['cid']=$cid;
                                          $bdata['if_oneself']=$v['if_oneself'];
                                          $bdata['sum_number']=$v['sum_number'];
                                          $bdata['surplus_sum_number']=$v['sum_number'];
                                          $bdata['if_main']=1;
                                           $bdata['if_distinguish']=0;
                                          if($if_distinguish>0){//是否区分平价日
                                            $bdata['holiday_number']=$holiday_number;
                                            $bdata['surplus_holiday_number']=$holiday_number;
                                            $bdata['ordinary_number']=$ordinary_number;
                                            $bdata['surplus_ordinary_number']=$ordinary_number;
                                            $bdata['if_distinguish']=$if_distinguish;
                                            
                                          }else{
                                            $bdata['holiday_number']=0;
                                            $bdata['surplus_holiday_number']=0;
                                            $bdata['ordinary_number']=0;
                                            $bdata['surplus_ordinary_number']=0;
                                            $bdata['if_distinguish']=0;
                                          }
                                          $bdata['day_max_number']=$v['day_max_number'];
                                          
                                          $bdata['years']=$years1;
                                          $bdata['card_number']=$data['card_number'];

                                          $bdata['effective_time']=$time;
                                          $bdata['expire_time']=strtotime("+$years1 year",$time);
                                          if (!empty($bdata)) {
                                          //$bdata=array_values($bdata);
                                            $res2=$db->table('benefit_member')->data($bdata)->insert();
                                          }
                                        
                                        
                                        ######
                                    }else{
                                          #####
                                          for ($i=1; $i<=$benefit_info['years'] ; $i++) {

                                            $end_time[0]=$time;

                                            $end_time[$i]=strtotime("+$i year",$time);
                                            $star=$end_time[$i-1];
                                            $end=$end_time[$i];
                                            $bdata['cycle_years']=$v['years'];
                                            $bdata['truename']=$data['truename'];
                                            $bdata['cycle_star_time']=$star;
                                            $bdata['cycle_end_time']=$end;
                                            $bdata['bid']=$bid;
                                            $bdata['boid']=$id;
                                            $bdata['uid']=$data['uid'];
                                            $bdata['cid']=$cid;
                                            $bdata['if_oneself']=$v['if_oneself'];
                                            $bdata['sum_number']=$v['sum_number'];
                                            $bdata['surplus_sum_number']=$v['sum_number'];
                                            $bdata['if_main']=1;
                                             $bdata['if_distinguish']=0;
                                            if($if_distinguish>0){//是否区分平价日
                                              $bdata['holiday_number']=$holiday_number;
                                              $bdata['surplus_holiday_number']=$holiday_number;
                                              $bdata['ordinary_number']=$ordinary_number;
                                              $bdata['surplus_ordinary_number']=$ordinary_number;
                                              $bdata['if_distinguish']=$if_distinguish;
                                              
                                            }else{
                                              $bdata['holiday_number']=0;
                                              $bdata['surplus_holiday_number']=0;
                                              $bdata['ordinary_number']=0;
                                              $bdata['surplus_ordinary_number']=0;
                                              $bdata['if_distinguish']=0;
                                            }
                                            $bdata['day_max_number']=$v['day_max_number'];
                                            $years1=intval($benefit_info['years']);
                                            $bdata['years']=$years1;
                                            $bdata['card_number']=$data['card_number'];

                                            $bdata['effective_time']=$time;
                                            $bdata['expire_time']=strtotime("+$years year",$time);
                                            if (!empty($bdata)) {
                                            //$bdata=array_values($bdata);
                                              $res2=$db->table('benefit_member')->data($bdata)->insert();
                                            }
                                          }
                                        
                                        ######
                                    }
                          }else{
                                    $benefit_data['bid']=$bid;
                                    $benefit_data['boid']=$id;
                                    $benefit_data['uid']=$data['uid'];
                                    $benefit_data['cid']=$cid;
                                    $benefit_data['sum_number']=$v['sum_number'];
                                    $benefit_data['surplus_sum_number']=$v['sum_number'];
                                    $benefit_data['if_main']=1;
                                    $benefit_data['if_distinguish']=0;
                                    if($if_distinguish>0){//是否区分平价日
                                      $benefit_data['holiday_number']=$holiday_number;
                                      $benefit_data['surplus_holiday_number']=$holiday_number;
                                      $benefit_data['ordinary_number']=$ordinary_number;
                                      $benefit_data['surplus_ordinary_number']=$ordinary_number;
                                      $benefit_data['if_distinguish']=$if_distinguish;
                                      
                                    }else{
                                      $benefit_data['holiday_number']=0;
                                      $benefit_data['surplus_holiday_number']=0;
                                      $benefit_data['ordinary_number']=0;
                                      $benefit_data['surplus_ordinary_number']=0;
                                      $benefit_data['if_distinguish']=0;
                                    }
                                    $benefit_data['day_max_number']=$v['day_max_number'];
                                    $years1=intval($benefit_info['years']);
                                    $benefit_data['truename']=$data['truename'];
                                    $benefit_data['if_oneself']=$v['if_oneself'];
                                    $benefit_data['years']=$years1;
                                    $benefit_data['card_number']=$data['card_number'];
                                    $benefit_data['cycle_years']=$v['years'];
                                    $benefit_data['cycle_star_time']=$time;
                                    $benefit_data['cycle_end_time']=strtotime("+$years year",$time);
                                    $benefit_data['effective_time']=$time;
                                    $benefit_data['expire_time']=strtotime("+$years year",$time);
                                    if (!empty($benefit_data)) {
                                      $res2=$db->table('benefit_member')->data($benefit_data)->insert();
                                    }
                          }
                              
                          ###########################
                      }

                    
                    
                  }
                }

              }

            }

            //结束
            
            $mobile=$data['mobphone'];
            //$time=time();
            $activate_time=date("Y年m月d日",time());
            //$time2=strtotime("+$years year");
            $end_time=date("Y年m月d日",strtotime("+$years year"));
            $content="【快乐岛高尔夫】尊敬的会员，您好！您已经购卡成功，您购买的是".$benefit_info['name']."会籍，可享受".$benefit_info['years']."年的球场会员待遇，购卡24小时后方可行权，详情咨询4000658786，祝您生活愉快";
            @sendmessage($mobile,$content);

            //短信提醒平台客服
              $ww['status']=1;
              $ww['type']=1;
              $service_congfig=$db->table('service_congfig')->where($ww)->select();
              if (!empty($service_congfig)) {
                  foreach ($service_congfig as $k=>$v) {
                      if (strlen($v['mobphone'])==11) {
                        $mobile3=$v['mobphone'];
                        $content3="【快乐岛高尔夫】客户".$data['truename'].'手机号：'.$mobile.'线上成功购买'.$benefit_info['name'].'会籍';
                        @sendmessage($mobile3,$content3);
                      }
                  }
              } 
          $wher222['id']=$id;
          $res1=$db->table('benefit_order')->data($data)->where($wher222)->update();
          return $res1;
          //var_dump($res1);
        }
     return;
}
function handlerefund($where,$time){
          $db=new db;
          $refund_info=$db->table('refund_log')->where($where)->find();
          //$myfile = fopen("refund.txt", "a");fwrite($myfile,var_export($refund_info,true));fclose($myfile);//创建日志
          //return $refund_info;die;
         if(!empty($refund_info)) {
                  $res22=array();
                  $res22['status']=1;
                  $res22['refund_time']=time();
                  $db->table('refund_log')->data($res22)->where($where)->update();
                  $refund_number=$refund_info['refund_buckle_number'];
                  $if_violate=$refund_info['if_violate'];
                   
                  if($refund_info['type']==0) {
                      $wher3['id']=$refund_info['oid'];
                      $wher3['order_number']=$refund_info['order_number'];
                      $booking_info=$db->table('booking_order')->where($wher3)->find();
                     //$myfile = fopen("updata_log22.txt", "a");fwrite($myfile,var_export($booking_info,true));fclose($myfile);//创建日志
                      //返还红包
                      if($booking_info['coupon_id']>0 && $refund_info['refund_coupon']>0){
                          $coupon_data['status']=0;
                          $wher44['id']=$booking_info['coupon_id'];
                          $db->table('coupon_receive_member')->data($coupon_data)->where($wher44)->update();
                      }
                      
                     if(!empty($booking_info) && $booking_info['refund_status']==2) {
                          $holiday=$booking_info['holiday'];
                          $cid=$booking_info['cid'];
                          $bid=$booking_info['bid'];
                          $uid=$booking_info['uid'];
                          $time=$booking_info['playing_time']; 
                          if($booking_info['type']==1) {//返还扣次
                              if($booking_info['buckle_number']>0 && $refund_number>0) {
                                  if($refund_number<=$booking_info['buckle_number']) {
                                       
                                        $wher23['cid']=$cid;
                                        $wher23['uid']=$uid;
                                        $wher23['bid']=$bid;
                                        $wher23['cycle_star_time']=array('elt',$time);
                                        $wher23['cycle_end_time']=array('egt',$time);
                                        $benefit_info=$db->table('benefit_member')->where($wher23)->find();
                                        if(!empty($benefit_info)) {
                                              $benefit_data['surplus_sum_number']= $benefit_info['surplus_sum_number']+$refund_number;
                                              $frozen_sum_number= $benefit_info['frozen_sum_number']-$refund_number;
                                              if ($frozen_sum_number<0) {
                                                $frozen_sum_number=0;
                                              }
                                              $benefit_data['frozen_sum_number']=$frozen_sum_number;
                                              

                                              if($benefit_info['if_distinguish']==1) {
                                                  if($holiday==1) {
                                                      $benefit_data['surplus_holiday_number']= $benefit_info['surplus_holiday_number']+$refund_number;
                                                      $frozen_holiday_number=$benefit_info['frozen_holiday_number']-$refund_number;
                                                      if($frozen_holiday_number<0) {
                                                        $frozen_holiday_number=0;
                                                      }

                                                      $benefit_data['frozen_holiday_number']=$frozen_holiday_number;
                                                  }else{
                                                      $benefit_data['surplus_ordinary_number']= $benefit_info['surplus_ordinary_number']+$refund_number;
                                                      $frozen_ordinary_number=$benefit_info['frozen_ordinary_number']-$refund_number;
                                                      if($frozen_ordinary_number<0) {
                                                        $frozen_ordinary_number=0;
                                                      }
                                                      $benefit_data['frozen_ordinary_number']=$frozen_ordinary_number-$refund_number;
                                                  }
                                              }
                                              $wher22['id']=$benefit_info['id'];
                                              $res=$db->table('benefit_member')->data($benefit_data)->where($wher22)->update();
                                                                    //生成记录
                                              if($res) {
                                                $account_log['cid']=$cid;
                                                $account_log['bid']=$bid;
                                                $account_log['oid']=$booking_info['id'];
                                                $account_log['uid']=$uid;
                                                if($refund_info['if_violate']==1) {
                                                  $account_log['type']=2;
                                                }else{
                                                  $account_log['type']=1;
                                                }
                                                
                                                $account_log['a_uid']=$refund_info['a_uid'];
                                                $account_log['holiday']=$holiday;
                                                $account_log['number']="+".$buckle_number;
                                                $account_log['addtime']=time();
                                                $db->table('benefit_account_log')->data($account_log)->insert();
                                              }

                                        }
                                  }
                              }
                          }
                          $wher212['id']=$booking_info['id'];
                          $booking2data22['refund_status']=1;
                          if($if_violate==0) {
                            $booking2data22['status']=3;
                          }else{
                            $booking2data22['status']=4;
                          }
                          
                          
                          $db->table('booking_order')->data($booking2data22)->where($wher212)->update();
                          //$myfile = fopen("updata_log.txt", "a");fwrite($myfile,var_export($booking2data22,true));fclose($myfile);//创建日志
                          $wh1['id']=$uid;
                          $members_data=$db->table('members')->where($wh1)->find();
                          $mobile=$members_data['mobphone'];
                           //短信提醒订场人
                          $wh['id']=$cid;
                          $court_data=$db->table('court')->where($wh)->find();
                          if (strlen($mobile)==11) {
                              $content="【快乐岛高尔夫】尊敬的客户，您好！您预订".date("Y年m月d日 H:i",$time)." ".$court_data['court_name']."，已退款成功。客服电话：4000658786";
                              @sendmessage($mobile,$content);
                          }

               
                          return;
                      }
                  return;
                  }
                  
              return;
              }

            return;  
}
